package tetris.game.view;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;


/**
 * Draws a new quadrat on the gamefield.
 * @author Adam Just
 *
 */
public class Quad {
	
	private float screen_width, screen_height, quad_size;
	private GL10 gl;
	private FloatBuffer quadVB;
	
	private final float FIELD_HEIGHT = 20.0f;

	
	public Quad(GL10 gl, float width, float height) {
		this.gl = gl;
		this.screen_width = width;
		this.screen_height = height;
		this.quad_size = this.screen_height / FIELD_HEIGHT;
	}

	
	/**
	 * Draws a quadrat at the position col, row
	 * @param col insert the column
	 * @param row insert the row
	 */
	public void drawQuad(int col, int row) {
		initQuad(col, row);

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, quadVB);
		gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
	}

	
	/**
	 * Inits the quad
	 * @param col
	 * @param row
	 */
	private void initQuad(int col, int row) {
		float quadCoords[] = { 
				col * quad_size, row * quad_size, 0, 								// bottom left
				col * quad_size, (row * quad_size) + quad_size, 0, 					// top left
				(col * quad_size) + quad_size, (row * quad_size) + quad_size, 0, 	// top right
				(col * quad_size) + quad_size, row * quad_size, 0 					// bottom right
		};

		// initialize vertex Buffer for quad
		ByteBuffer vbb = ByteBuffer.allocateDirect(quadCoords.length * 4);
		vbb.order(ByteOrder.nativeOrder());		// use the device hardware's native byte order
		quadVB = vbb.asFloatBuffer(); 			// create a floating point buffer from the ByteBuffer
		quadVB.put(quadCoords); 				// add the coordinates to the FloatBuffer
		quadVB.position(0); 					// set the buffer to read the first coordinate
	}
}